﻿#include <iostream>
#include<cmath>
using namespace std;

float vect(float vx1, float vy1, float vx2, float vy2)// 向量计算
{
    float res = 0.5 * ((vx1 * vy2) - (vy1 * vx2));
    if (res <= 0)
    {
        res = -res;
    }
    return res;
}

static bool interj1(float y1, float k1, float x1, float y2, float k2, float x2, float y3,
    float k3,float x3,float& xa, float& ya,float &xb,float &yb,float &xc,float &yc,float &s)// 点斜式计算公式
{
    if (k1 == k2 || k2 == k3 || k3 == k1)
    {
        cout << "斜率相等。不存在这样的三角形。";
        return false;
    }
    xa = (k1 * x1 - k2 * x2 + y2 - y1) / (k1 - k2);
    ya = k1 * (xa - x1) + y1;
    xb = (k2 * x2 - k3 * x3 + y3 - y2) / (k2 - k3);
    yb = k2 * (xb - x2) + y2;
    float dx1, dy1, dx2, dy2;
    xc = (k1 * x1 - k3 * x3 + y3 - y1) / (k1 - k3);
    yc = k1 * (xc - x1) + y1;
    dx1 = xa - xb;
    dx2 = xb - xc;
    dy1 = ya - yb;
    dy2 = yb - yc;
    vect(dx1,dy1,dx2,dy2);
    s = vect(dx1, dy1,  dx2, dy2);
    return true;
}

static bool interj2(float k1, float b1, float k2, float b2, float k3,float b3, float& xa, 
    float& ya, float& xb, float& yb, float& xc, float& yc, float& s)// 斜截式计算公式
{
    if (k1 == k2 || k2 == k3 || k3 == k1)
    {
        cout << "斜率相等。不存在这样的三角形。";
        return false;
    }
    float dx1, dy1, dx2, dy2;
    xa = (b2 - b1) / (k1 - k2);
    ya = k1 * xa + b1;
    xb = (b3 - b1) / (k1 - k3);
    yb = k1 * xb + b1;
    xc = (b2 - b3) / (k3 - k2);
    yc = k3 * xc + b3;
    dx1 = xa - xb;
    dx2 = xb - xc;
    dy1 = ya - yb;
    dy2 = yb - yc;
    vect(dx1,  dy1,  dx2,  dy2);
    s = vect( dx1,  dy1, dx2,  dy2);
    return true;
}

static bool interj3(float k1, float b1, float k2, float b2,float b3, float& xa,
    float& ya, float& xb, float& yb, float& xc, float& yc, float& s)// 斜截式计算公式,b3 is 不存在的斜率，xa为存在的囧啊点
{
    if (k1 == k2 )
    {
        cout << "斜率相等。不存在这样的三角形。";
        return false;
    }
    float dx,dy;
    xa = (b2 - b1) / (k1 - k2);
    ya = k1 * xa + b1;
    xb = b3;
    yb = k1 * b3 + b1;
    xc = b3 ;
    yc = k2 * b3 + b2;
    dy = yc > yb ? yc - yb : yb - yc;
    dx = b3 > xa ? b3 - xa : xa - b3;
    s = 0.5 * (dx * dy);
    return true;
}

int main()
{
    int m;
    float y1, k1, x1, y2, k2, x2, y3, k3, x3, b1, b2, b3, xa, ya, xb, yb, xc, yc;
    cout << "求交点三角形面积。函数点斜式表达按1，函数斜截式表达按2。另外，如果斜率不存在请按3（仅支持点斜式）。此时仅需两组数据,第三组数据请填写x=b中的b值"
        << "\n\n\n";
    cout << "选择模式：";
    cin >> m;
    switch (m)
    {
    case 1:
    {
        cout << "请输入第一组数据;";
        cin >> y1 >> k1 >> x1;
        cout << "请输入第二组数据;";
        cin >> y2 >> k2 >> x2;
        cout << "请输入第三组数据;";
        cin >> y3 >> k3 >> x3;
        float s;
        if (interj1(y1, k1, x1, y2, k2, x2, y3, k3, x3, xa, ya, xb, yb, xc, yc, s) == true)
        {
            cout << "交点坐标：" << "\n" << s;
        }
        return 0;
    }
    case 2:
    {
        cout << "请输入第一组数据;";
        cin >> k1 >> b1;
        cout << "请输入第二组数据;";
        cin >> k2 >> b2;
        cout << "请输入第三组数据";
        cin >> k3 >> b3;
        float s;
        if (interj2(k1, b1, k2, b2, k3, b3, xa, ya, xb, yb, xc, yc, s) == true)
        {
            cout << "三角形面积" << "\n\n\n" << s;
        }
        return 0;
    }
    return 0;
    case 3:
    {
        cout << "请输入第一组数据;";
        cin >> k1 >> b1;
        cout << "请输入第二组数据;";
        cin >> k2 >> b2;
        cout << "请输入第三组数据";
        cin >> b3;
        float s;
        if (interj3(k1, b1, k2, b2, b3, xa, ya, xb, yb, xc, yc, s) == true)
        {
            cout << "三角形面积" << "\n\n\n" << s;
        }
        return 0;
    }
    }
}